import uuid
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS

from utils.logger import logger

from models.database import db

app = Flask(__name__)
CORS(app)  # 允许所有域访问

# 配置数据库
app.config.from_object('config.Config')  # or any database you use
# 初始化数据库
db.init_app(app)

with app.app_context():  # Wrap in app context
    # 在应用上下文中导入模型，确保 db 已初始化
    from models import article # 导入模型，自动创建表
    db.create_all()  # Creates database tables
    # 检查表是否存在
    # from sqlalchemy import inspect
    # inspector = inspect(db.engine)
    # tables = inspector.get_table_names()
    # logger.info(f"数据库中的表: {tables}")    

#导入路由
from routes.task_routes import task_bp
app.register_blueprint(task_bp, url_prefix='/v1')
from routes.article_routes import article_bp
app.register_blueprint(article_bp, url_prefix='/v1')



logger.debug("\nRegistered Routes:")
for rule in app.url_map.iter_rules():
    methods = ','.join(rule.methods)
    logger.debug(f"{rule.endpoint}: {rule.rule} [{methods}]")
logger.info(f"DB 000 Instance ID: {id(db)}")  # 所有模块应输出相同 ID
# app.run(debug=True,port=7090,host='0.0.0.0')
   
